package com.ssy.lingxi.pay.controller.feign;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ssy.lingxi.common.constant.Constants;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.component.redis.service.IRedisStringUtils;
import com.ssy.lingxi.pay.api.common.EAccountPayChannelEnum;
import com.ssy.lingxi.pay.api.common.EAccountPayTypeEnum;
import com.ssy.lingxi.pay.api.common.allInPay.AllInPayPayMethodEnum;
import com.ssy.lingxi.pay.api.common.allInPay.EAccountPayStatusEnum;
import com.ssy.lingxi.pay.api.dto.request.eAccount.*;
import com.ssy.lingxi.pay.api.model.vo.request.allInPay.*;
import com.ssy.lingxi.pay.api.model.vo.response.allInPay.*;
import com.ssy.lingxi.pay.common.PayTypeEnum;
import com.ssy.lingxi.pay.model.vo.request.GateWayPayVO;
import com.ssy.lingxi.pay.service.allInPay.IAllInPayService;
import com.ssy.lingxi.pay.service.eAccount.IEAccountService;
import com.ssy.lingxi.pay.utils.WeChatPayUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lqa
 * @version 2.0.0
 * @menu 通联支付-内部调用接口
 * @date 2021/12/7
 */
@RestController
@RequestMapping("/pay/allInPay")
public class AllInPayFeignController extends BaseController {

    @Resource
    private IAllInPayService payService;

    @Resource
    private IEAccountService eAccountService;

    @Resource
    private HttpServletRequest request;

    @Resource
    private IRedisStringUtils redisStringUtils;

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 通联支付-订单分账退款申请
     * 若未发生分账，不允许调用该接口
     *
     * @return 操作结果
     */
    @RequestMapping(value = "/orderSplitRefund", method = RequestMethod.POST)
    public Wrapper<OrderSplitRefundRespVO> orderSplitRefund(@RequestBody @Valid OrderSplitRefundVO vo) {
        return Wrapper.success(payService.orderSplitRefund(vo));
    }

    /**
     * 通联支付-平台转账
     *
     * @return 操作结果
     */
    @RequestMapping(value = "/applicationTransfer", method = RequestMethod.POST)
    public Wrapper<ApplicationTransferRespVO> applicationTransfer(@RequestBody @Valid ApplicationTransferVO vo) {
        return Wrapper.success(payService.applicationTransfer(vo));
    }

    /**
     * 通联支付-退款申请
     *
     * @return 操作结果
     */
    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    public Wrapper<RefundRespVO> refund(@RequestBody @Valid RefundVO vo) {
        return Wrapper.success(payService.refund(vo));
    }

    /**
     * 通联支付-批量托管代付（标准版）(付款到账户余额)
     *
     * @return 操作结果 商户订单号
     */
    @RequestMapping(value = "/batchAgentPay", method = RequestMethod.POST)
    public Wrapper<String> batchAgentPay(@RequestBody @Valid BatchAgentPayVO vo) {
        return Wrapper.success(payService.batchAgentPay(vo));
    }


    /**
     * 通联支付-单笔托管代付（标准版）(付款到账户余额)
     *
     * @return 操作结果
     */
    @RequestMapping(value = "/signalAgentPay", method = RequestMethod.POST)
    public Wrapper<SignalAgentPayRespVO> signalAgentPay(@RequestBody @Valid SignalAgentPayVO vo) {
        return Wrapper.success(payService.signalAgentPay(vo));
    }

    /**
     * 通联支付-托管代收申请（标准版）(平台代收)
     * 1)托管代收交易即将用户（买方）资金代收到中间账户。
     * 2) 托管代收申请成功后，需要支付确认。
     *
     * @return 操作结果
     */
    @RequestMapping(value = "/agentCollectApply", method = RequestMethod.POST)
    public Wrapper<ConsumeApplyRespVO> agentCollectApply(@RequestBody @Valid AgentCollectApplyVO vo) {
        //1-如果是余额支付，则PC，APP，小程序通用
        if (AllInPayPayMethodEnum.BALANCE.getPayMethod().equals(vo.getPayMethodStr())) {
            return Wrapper.success(payService.balanceAgentCollectApply(vo));
        } else {
            //2.1-支付宝或者微信PC正扫（集团）支付,微信小程序(收银宝),支付宝或者微信PC正扫支付,收银宝网关支付,收银宝快捷支付
            if (Stream.of(AllInPayPayMethodEnum.SCAN_WEI_XIN_ORG.getPayMethod(),//微信正扫(集团)
                    AllInPayPayMethodEnum.SCAN_ALIPAY_ORG.getPayMethod(),//支付宝正扫(集团)
                    AllInPayPayMethodEnum.WE_CHAT_PAY_MINI_PROGRAM_ORG.getPayMethod(),//收银宝微信小程序支付(集团)
                    AllInPayPayMethodEnum.SCAN_WEI_XIN.getPayMethod(),//微信正扫
                    AllInPayPayMethodEnum.SCAN_ALIPAY.getPayMethod(),//支付宝正扫
                    AllInPayPayMethodEnum.GATE_WAY.getPayMethod(),//网关支付
                    AllInPayPayMethodEnum.QUICKPAY_VSP.getPayMethod(),//收银宝快捷支付
                    AllInPayPayMethodEnum.GATEWAY_VSP.getPayMethod()//收银宝网关支付
            ).collect(Collectors.toList()).contains(vo.getPayMethodStr())) {
                return Wrapper.success(payService.scanAgentCollectApply(vo));
            } else {
                //预留支付方式
                return Wrapper.success();
            }
        }
    }

    /**
     * 通联支付-消费申请(商户直接到账)
     *
     * @return 操作结果
     */
    @RequestMapping(value = "/consumeApply", method = RequestMethod.POST)
    public Wrapper<ConsumeApplyRespVO> consumeApply(@RequestBody @Valid ConsumeApplyVO vo) {
        //1-如果是余额支付，则PC，APP，小程序通用
        if (AllInPayPayMethodEnum.BALANCE.getPayMethod().equals(vo.getPayMethodStr())) {
            return Wrapper.success(payService.balanceConsumeApply(vo));
        } else {
            //2.1-支付宝或者微信PC正扫（集团）支付,微信小程序(收银宝),,
            if (Stream.of(AllInPayPayMethodEnum.SCAN_WEI_XIN_ORG.getPayMethod(),//微信正扫(集团)
                    AllInPayPayMethodEnum.SCAN_ALIPAY_ORG.getPayMethod(),//支付宝正扫(集团)
                    AllInPayPayMethodEnum.WE_CHAT_PAY_MINI_PROGRAM_ORG.getPayMethod(),//收银宝微信小程序支付(集团)
                    AllInPayPayMethodEnum.SCAN_WEI_XIN.getPayMethod(),//微信正扫
                    AllInPayPayMethodEnum.SCAN_ALIPAY.getPayMethod(),//支付宝正扫
                    AllInPayPayMethodEnum.GATE_WAY.getPayMethod(),//网关支付
                    AllInPayPayMethodEnum.QUICKPAY_VSP.getPayMethod(),//收银宝快捷支付
                    AllInPayPayMethodEnum.GATEWAY_VSP.getPayMethod()//收银宝网关支付
            ).collect(Collectors.toList()).contains(vo.getPayMethodStr())) {
                return Wrapper.success(payService.scanBalanceConsumeApply(vo));
            } else {
                //预留支付方式
                return Wrapper.success();
            }

        }
    }

    /**
     * 通联支付-提现申请
     *
     * @return 操作结果
     */
    @RequestMapping(value = "/withdrawApply", method = RequestMethod.POST)
    public Wrapper<WithdrawApplyRespVO> withdrawApply(@RequestBody WithdrawApplyVO vo) {
        return Wrapper.success(payService.withdrawApply(getSysUser(), vo));
    }

    /**
     * 通联支付-充值申请
     *
     * @return 操作结果
     */
    @RequestMapping(value = "/depositApply", method = RequestMethod.POST)
    public Wrapper<DepositApplyRespVO> depositApply(@RequestBody DepositApplyVO vo) {
        return Wrapper.success(payService.depositApply(getSysUser(), vo));
    }

    /**
     * 订单支付
     *
     * @param eAccountOrderPayRequest 接口参数
     */
    @RequestMapping(value = "/orderPay", method = RequestMethod.POST)
    public Wrapper<String> orderPay(@RequestBody @Valid EAccountOrderPayRequest eAccountOrderPayRequest) {
        Integer payChannel = eAccountOrderPayRequest.getPayChannel();
        log.error("==========payChannel:" + payChannel);

        Integer payType = eAccountOrderPayRequest.getPayType();
        if (PayTypeEnum.Platform.getCode().equals(payType)) {
            //平台代收
            AgentCollectApplyVO agentCollectApplyVO = new AgentCollectApplyVO();
            agentCollectApplyVO.setMemberId(eAccountOrderPayRequest.getPayMemberId());
            agentCollectApplyVO.setMemberRoleId(eAccountOrderPayRequest.getPayMemberRoleId());
            agentCollectApplyVO.setBizOrderNo(eAccountOrderPayRequest.getOrderCode());
            agentCollectApplyVO.setAmount(eAccountOrderPayRequest.getPayMoney());
            agentCollectApplyVO.setExtendInfo(eAccountOrderPayRequest.getAttach());
            agentCollectApplyVO.setNonce(eAccountOrderPayRequest.getNonce());
            agentCollectApplyVO.setServiceType(eAccountOrderPayRequest.getServiceType());
            //收款人
            List<RecieverVO> receiveVOList;
            List<ReceiveRequest> receiveList = eAccountOrderPayRequest.getReceiveList();
            if (receiveList != null && receiveList.size() > 0) {
                receiveVOList = receiveList.stream().map(receiveRequest -> {
                    RecieverVO recieverVO = new RecieverVO();
                    recieverVO.setMemberId(receiveRequest.getMemberId());
                    recieverVO.setMemberRoleId(receiveRequest.getMemberRoleId());
                    recieverVO.setAmount(receiveRequest.getPayMoney());
                    return recieverVO;
                }).collect(Collectors.toList());
            } else {
                receiveVOList = new ArrayList<>();
                RecieverVO recieverVO = new RecieverVO();
                recieverVO.setMemberId(eAccountOrderPayRequest.getMemberId());
                recieverVO.setMemberRoleId(eAccountOrderPayRequest.getMemberRoleId());
                recieverVO.setAmount(eAccountOrderPayRequest.getPayMoney());
                receiveVOList.add(recieverVO);
            }
            agentCollectApplyVO.setRecieverList(receiveVOList);

            if (EAccountPayChannelEnum.Platform_Wechat.getCode().equals(payChannel)) {
                //PC端微信
                agentCollectApplyVO.setPayMethodStr(EAccountPayTypeEnum.SCAN_WEIXIN.getCode());
                ConsumeApplyRespVO consumeApplyRespVO = payService.scanAgentCollectApply(agentCollectApplyVO);
                return Wrapper.success(consumeApplyRespVO.getPayInfo());
            } else if (EAccountPayChannelEnum.Platform_Alipay.getCode().equals(payChannel)) {
                //PC端支付宝
                agentCollectApplyVO.setPayMethodStr(EAccountPayTypeEnum.SCAN_ALIPAY.getCode());
                ConsumeApplyRespVO consumeApplyRespVO = payService.scanAgentCollectApply(agentCollectApplyVO);
                return Wrapper.success(consumeApplyRespVO.getPayInfo());
            } else if (EAccountPayChannelEnum.APP_Wechat.getCode().equals(payChannel) || EAccountPayChannelEnum.Applet_Wechat.getCode().equals(payChannel)) {
                //APP微信
                agentCollectApplyVO.setPayMethodStr(EAccountPayTypeEnum.WECHATPAY_MINIPROGRAM_ORG.getCode());
                String jsCode = eAccountOrderPayRequest.getJsCode();
                if (StringUtils.isEmpty(jsCode)) {
                    throw new BusinessException(ResponseCode.PAY_E_ACCOUNT_CODE_NOT_EXIST);
                }
                Map<String, String> param = eAccountService.getWechatParam(jsCode);
                agentCollectApplyVO.setAcct(param.get("openId"));
                agentCollectApplyVO.setSubAppId(param.get("subAppId"));
                agentCollectApplyVO.setVspCusId(param.get("merchantId"));
                ConsumeApplyRespVO consumeApplyRespVO = payService.scanAgentCollectApply(agentCollectApplyVO);
                return Wrapper.success(consumeApplyRespVO.getPayInfo());
            } else if (EAccountPayChannelEnum.APP_Alipay.getCode().equals(payChannel)) {
                //APP支付宝
                agentCollectApplyVO.setPayMethodStr(EAccountPayTypeEnum.SCAN_ALIPAY.getCode());
                ConsumeApplyRespVO consumeApplyRespVO = payService.scanAgentCollectApply(agentCollectApplyVO);
                return Wrapper.success(consumeApplyRespVO.getPayInfo());
            } else if (EAccountPayChannelEnum.Balance.getCode().equals(payChannel)) {
                //余额
                agentCollectApplyVO.setPayMethodStr(EAccountPayTypeEnum.BALANCE.getCode());
                agentCollectApplyVO.setValidateType(1);
                ConsumeApplyRespVO consumeApplyRespVO = payService.balanceAgentCollectApply(agentCollectApplyVO);
                return Wrapper.success(consumeApplyRespVO.getPayInfo());
            } else if (EAccountPayChannelEnum.Quick_Pay.getCode().equals(payChannel)) {
                //快捷支付
                agentCollectApplyVO.setPayMethodStr(EAccountPayTypeEnum.QUICKPAY_VSP.getCode());
                agentCollectApplyVO.setValidateType(1);
                ConsumeApplyRespVO consumeApplyRespVO = payService.scanAgentCollectApply(agentCollectApplyVO);
                String payStatus = consumeApplyRespVO.getPayStatus();
                if (EAccountPayStatusEnum.FAIL.getCode().equals(payStatus) || EAccountPayStatusEnum.UNPAY.getCode().equals(payStatus)) {
                    return Wrapper.fail(consumeApplyRespVO.getPayFailMessage());
                } else {
                    return Wrapper.success(consumeApplyRespVO.getPayInfo());
                }
            } else if (EAccountPayChannelEnum.GateWay_Pay.getCode().equals(payChannel)) {
                //银联支付
                agentCollectApplyVO.setPayMethodStr(EAccountPayTypeEnum.GATEWAY_VSP.getCode());
                ConsumeApplyRespVO consumeApplyRespVO = payService.scanAgentCollectApply(agentCollectApplyVO);
                if (!ObjectUtils.isEmpty(consumeApplyRespVO)) {
                    String remoteAddrIp = WeChatPayUtil.getRemoteAddrIp(request);
                    GateWayPayVO gateWayPayVO = new GateWayPayVO();
                    gateWayPayVO.setBizOrderNo(eAccountOrderPayRequest.getOrderCode());
                    gateWayPayVO.setConsumerIp(remoteAddrIp);
                    return Wrapper.success(payService.gateWayPay(eAccountOrderPayRequest.getPayMemberId(), eAccountOrderPayRequest.getPayMemberRoleId(), gateWayPayVO));
                } else {
                    return Wrapper.fail(consumeApplyRespVO.getPayFailMessage());
                }
            } else if (EAccountPayChannelEnum.JsApi_Wechat.getCode().equals(payChannel)) {
                //h5微信支付
                agentCollectApplyVO.setPayMethodStr(EAccountPayTypeEnum.WECHATPAY_H5_OPEN.getCode());

                String subAppId = eAccountService.getWechatSubAppId();
                agentCollectApplyVO.setSubAppId(subAppId);
                // agentCollectApplyVO.setCusIp(eAccountOrderPayRequest.getCusIp());
                ConsumeApplyRespVO consumeApplyRespVO = payService.scanAgentCollectApply(agentCollectApplyVO);
                return Wrapper.success(consumeApplyRespVO.getPayInfo());
            }
        } else if (PayTypeEnum.Member.getCode().equals(payType)) {
            //会员直接到账
            ConsumeApplyVO consumeApplyVO = new ConsumeApplyVO();
            consumeApplyVO.setMemberId(eAccountOrderPayRequest.getPayMemberId());
            consumeApplyVO.setMemberRoleId(eAccountOrderPayRequest.getPayMemberRoleId());
            consumeApplyVO.setRecieverMemberId(eAccountOrderPayRequest.getMemberId());
            consumeApplyVO.setRecieverMemberRoleId(eAccountOrderPayRequest.getMemberRoleId());
            consumeApplyVO.setBizOrderNo(eAccountOrderPayRequest.getOrderCode());
            consumeApplyVO.setAmount(eAccountOrderPayRequest.getPayMoney());
            consumeApplyVO.setExtendInfo(eAccountOrderPayRequest.getAttach());
            consumeApplyVO.setNonce(eAccountOrderPayRequest.getNonce());
            consumeApplyVO.setServiceType(eAccountOrderPayRequest.getServiceType());
            if (EAccountPayChannelEnum.Platform_Wechat.getCode().equals(payChannel)) {
                //PC端微信
                consumeApplyVO.setPayMethodStr(EAccountPayTypeEnum.SCAN_WEIXIN.getCode());
                ConsumeApplyRespVO consumeApplyRespVO = payService.scanBalanceConsumeApply(consumeApplyVO);
                return Wrapper.success(consumeApplyRespVO.getPayInfo());
            } else if (EAccountPayChannelEnum.Platform_Alipay.getCode().equals(payChannel)) {
                //PC端支付宝
                consumeApplyVO.setPayMethodStr(EAccountPayTypeEnum.SCAN_ALIPAY.getCode());
                ConsumeApplyRespVO consumeApplyRespVO = payService.scanBalanceConsumeApply(consumeApplyVO);
                return Wrapper.success(consumeApplyRespVO.getPayInfo());
            } else if (EAccountPayChannelEnum.APP_Wechat.getCode().equals(payChannel) || EAccountPayChannelEnum.Applet_Wechat.getCode().equals(payChannel)) {
                //APP微信
                consumeApplyVO.setPayMethodStr(EAccountPayTypeEnum.WECHATPAY_MINIPROGRAM_ORG.getCode());
                String jsCode = eAccountOrderPayRequest.getJsCode();
                if (StringUtils.isEmpty(jsCode)) {
                    throw new BusinessException(ResponseCode.PAY_E_ACCOUNT_CODE_NOT_EXIST);
                }
                Map<String, String> param = eAccountService.getWechatParam(jsCode);
                consumeApplyVO.setAcct(param.get("openId"));
                consumeApplyVO.setVspCusId(param.get("merchantId"));
                consumeApplyVO.setSubAppId(param.get("subAppId"));
                ConsumeApplyRespVO consumeApplyRespVO = payService.scanBalanceConsumeApply(consumeApplyVO);
                return Wrapper.success(consumeApplyRespVO.getPayInfo());
            } else if (EAccountPayChannelEnum.APP_Alipay.getCode().equals(payChannel)) {
                //APP支付宝
                consumeApplyVO.setPayMethodStr(EAccountPayTypeEnum.SCAN_ALIPAY.getCode());
                ConsumeApplyRespVO consumeApplyRespVO = payService.scanBalanceConsumeApply(consumeApplyVO);
                return Wrapper.success(consumeApplyRespVO.getPayInfo());
            } else if (EAccountPayChannelEnum.Balance.getCode().equals(payChannel)) {
                //余额
                consumeApplyVO.setPayMethodStr(EAccountPayTypeEnum.BALANCE.getCode());
                consumeApplyVO.setValidateType(1);
                ConsumeApplyRespVO consumeApplyRespVO = payService.balanceConsumeApply(consumeApplyVO);
                return Wrapper.success(consumeApplyRespVO.getPayInfo());
            } else if (EAccountPayChannelEnum.Quick_Pay.getCode().equals(payChannel)) {
                //快捷支付
                consumeApplyVO.setPayMethodStr(EAccountPayTypeEnum.QUICKPAY_VSP.getCode());
                consumeApplyVO.setValidateType(1);
                ConsumeApplyRespVO consumeApplyRespVO = payService.scanBalanceConsumeApply(consumeApplyVO);
                String payStatus = consumeApplyRespVO.getPayStatus();
                if (EAccountPayStatusEnum.FAIL.getCode().equals(payStatus) || EAccountPayStatusEnum.UNPAY.getCode().equals(payStatus)) {
                    return Wrapper.fail(consumeApplyRespVO.getPayFailMessage());
                } else {
                    return Wrapper.success(consumeApplyRespVO.getPayInfo());
                }
            } else if (EAccountPayChannelEnum.GateWay_Pay.getCode().equals(payChannel)) {
                //银联支付
                consumeApplyVO.setPayMethodStr(EAccountPayTypeEnum.GATEWAY_VSP.getCode());
                ConsumeApplyRespVO consumeApplyRespVO = payService.scanBalanceConsumeApply(consumeApplyVO);
                if (!ObjectUtils.isEmpty(consumeApplyRespVO)) {
                    String remoteAddrIp = WeChatPayUtil.getRemoteAddrIp(request);
                    GateWayPayVO gateWayPayVO = new GateWayPayVO();
                    gateWayPayVO.setBizOrderNo(eAccountOrderPayRequest.getOrderCode());
                    gateWayPayVO.setConsumerIp(remoteAddrIp);
                    return Wrapper.success(payService.gateWayPay(eAccountOrderPayRequest.getPayMemberId(), eAccountOrderPayRequest.getPayMemberRoleId(), gateWayPayVO));
                } else {
                    return Wrapper.fail(consumeApplyRespVO.getPayFailMessage());
                }
            } else if (EAccountPayChannelEnum.JsApi_Wechat.getCode().equals(payChannel)) {
                //h5微信支付
                consumeApplyVO.setPayMethodStr(EAccountPayTypeEnum.WECHATPAY_H5_OPEN.getCode());
                String subAppId = eAccountService.getWechatSubAppId();
                consumeApplyVO.setSubAppId(subAppId);
                //  consumeApplyVO.setCusIp(eAccountOrderPayRequest.getCusIp());
                ConsumeApplyRespVO consumeApplyRespVO = payService.scanBalanceConsumeApply(consumeApplyVO);
                return Wrapper.success(consumeApplyRespVO.getPayInfo());
            }
        }
        throw new BusinessException(ResponseCode.PAY_E_ACCOUNT_NOT_SUPPORT);
    }

    /**
     * 平台代付
     *
     * @param platFormRePayRequest 参数
     */
    @RequestMapping(value = "/platFormRePay", method = RequestMethod.POST)
    public Wrapper<SignalAgentPayRespVO> platFormRePay(@RequestBody @Valid PlatFormRePayRequest platFormRePayRequest) {
        SignalAgentPayVO signalAgentPayVO = new SignalAgentPayVO();
        signalAgentPayVO.setAmount(platFormRePayRequest.getPayMoney());
        signalAgentPayVO.setBizOrderNo(platFormRePayRequest.getOrderCode());
        signalAgentPayVO.setMemberId(platFormRePayRequest.getMemberId());
        signalAgentPayVO.setMemberRoleId(platFormRePayRequest.getMemberRoleId());
        List<CollectPayVO> receiverVOList;
        List<ReceivePaymentRequest> receiveList = platFormRePayRequest.getReceiveList();
        if (receiveList != null && receiveList.size() > 0) {
            receiverVOList = receiveList.stream().map(receiveRequest -> {
                CollectPayVO collectPayVO = new CollectPayVO();
                collectPayVO.setBizOrderNo(receiveRequest.getBizOrderNo());
                collectPayVO.setAmount(new BigDecimal(receiveRequest.getPayMoney()));
                return collectPayVO;
            }).collect(Collectors.toList());
            signalAgentPayVO.setCollectPayList(receiverVOList);
        }
        return Wrapper.success(payService.signalAgentPay(signalAgentPayVO));
    }

    /**
     * 平台转账
     *
     * @param platFormTransferRequest 参数
     */
    @RequestMapping(value = "/platFormTransfer", method = RequestMethod.POST)
    public Wrapper<ApplicationTransferRespVO> platFormTransfer(@RequestBody @Valid PlatFormTransferRequest platFormTransferRequest) {
        ApplicationTransferVO applicationTransferVO = new ApplicationTransferVO();
        applicationTransferVO.setMemberId(platFormTransferRequest.getMemberId());
        applicationTransferVO.setMemberRoleId(platFormTransferRequest.getMemberRoleId());
        applicationTransferVO.setAmount(platFormTransferRequest.getPayMoney());
        applicationTransferVO.setBizTransferNo(platFormTransferRequest.getOrderCode());
        return Wrapper.success(payService.applicationTransfer(applicationTransferVO));
    }

    /**
     * 退款申请(1-会员直接到账; 2-平台代收并且平台还没结算给商家)
     *
     * @param eAccountRefundRequest 参数
     */
    @RequestMapping(value = "/orderRefund", method = RequestMethod.POST)
    public Wrapper<RefundRespVO> orderRefund(@RequestBody @Valid EAccountRefundRequest eAccountRefundRequest) {
        RefundVO refundVO = new RefundVO();
        refundVO.setAmount(eAccountRefundRequest.getRefundMoney());
        refundVO.setBizOrderNo(eAccountRefundRequest.getOrderCode());
        refundVO.setOriBizOrderNo(eAccountRefundRequest.getPayCode());
        refundVO.setMemberId(eAccountRefundRequest.getMemberId());
        refundVO.setMemberRoleId(eAccountRefundRequest.getMemberRoleId());
        if (eAccountRefundRequest.getRefundType() == 1) { //只有部分退款才会用到此字段
            RefundPayeeVO vo = new RefundPayeeVO();
            vo.setAmount(new BigDecimal(eAccountRefundRequest.getRefundMoney()).multiply(new BigDecimal("100")).doubleValue());
            vo.setMemberId(eAccountRefundRequest.getReMemberId());
            vo.setMemberRoleId(eAccountRefundRequest.getReMemberRoleId());
            vo.setBizUserId(eAccountRefundRequest.getReMemberId() + "__" + eAccountRefundRequest.getReMemberRoleId());
            List<RefundPayeeVO> refundPayeeVOS = new ArrayList<>();
            refundPayeeVOS.add(vo);
            refundVO.setRefundList(refundPayeeVOS);
        }
        return Wrapper.success(payService.refund(refundVO));
    }

    /**
     * 退款申请(平台代收并且平台已经结算给商家)
     *
     * @param storeRefundRequest 参数
     */
    @RequestMapping(value = "/orderStoreRefund", method = RequestMethod.POST)
    public Wrapper<OrderSplitRefundRespVO> orderStoreRefund(@RequestBody @Valid StoreRefundRequest storeRefundRequest) {
        OrderSplitRefundVO orderSplitRefundVO = new OrderSplitRefundVO();
        orderSplitRefundVO.setBizOrderNo(storeRefundRequest.getOrderCode());
        orderSplitRefundVO.setOriBizOrderNo(storeRefundRequest.getPayCode());
        orderSplitRefundVO.setMemberId(storeRefundRequest.getPayMemberId());
        orderSplitRefundVO.setMemberRoleId(storeRefundRequest.getPayMemberRoleId());

        List<StoreRefundReceiveRequest> storeRefundReceiveList = storeRefundRequest.getStoreRefundReceiveList();
        List<OrderRefundVO> orderRefundList = storeRefundReceiveList.stream().map(eAccountRefundReceiveRequest -> {
            OrderRefundVO orderRefundVO = new OrderRefundVO();
            orderRefundVO.setSplitBizOrderNo(eAccountRefundReceiveRequest.getBizOrderNo());
            orderRefundVO.setAmount(eAccountRefundReceiveRequest.getPayMoney());
            orderRefundVO.setTotalSplitAmount(eAccountRefundReceiveRequest.getTotalSplitAmount());

            List<StoreRefundReceiveMemberRequest> storeRefundReceiveMemberList = eAccountRefundReceiveRequest.getStoreRefundReceiveMemberList();
            List<SplitRefundVO> splitRefundList = storeRefundReceiveMemberList.stream().map(eAccountRefundReceiveMemberRequest -> {
                SplitRefundVO splitRefundVO = new SplitRefundVO();
                splitRefundVO.setAmount(eAccountRefundReceiveMemberRequest.getRefundMoney());
                splitRefundVO.setMemberId(eAccountRefundReceiveMemberRequest.getMemberId());
                splitRefundVO.setMemberRoleId(eAccountRefundReceiveMemberRequest.getMemberRoleId());
                splitRefundVO.setRemark(eAccountRefundReceiveMemberRequest.getRemark());
                return splitRefundVO;
            }).collect(Collectors.toList());
            orderRefundVO.setSplitRefundList(splitRefundList);
            return orderRefundVO;
        }).collect(Collectors.toList());
        orderSplitRefundVO.setOrderRefundList(orderRefundList);
        return Wrapper.success(payService.orderSplitRefund(orderSplitRefundVO));
    }


    @RequestMapping(value = "/cacheParams", method = RequestMethod.POST)
    public Wrapper<Void> cacheParams(@RequestBody @Valid List<CacheParamsRequest> cacheParamsRequest) {
        try {
            final String key = "allInPay";
            Map<Integer, String> parameters = cacheParamsRequest.stream().collect(Collectors.toMap(CacheParamsRequest::getCode, CacheParamsRequest::getValue));
            ObjectMapper mapper = new ObjectMapper();
            String cacheParams = mapper.writeValueAsString(parameters);
            Boolean cacheSuccess = redisStringUtils.set(key, cacheParams, 0L, Constants.REDIS_PAY_INDEX);
            log.info("缓存通联配置参数, cacheParams:" + cacheParams + ", 缓存结果: " + cacheSuccess);
            String value = redisStringUtils.get(key, Constants.REDIS_PAY_INDEX);

            log.info("缓存参数value:" + value);
        } catch (Exception e) {
            log.error("缓存订单支付参数错误：" + e.getMessage());
        }
        return Wrapper.success();
    }
}
